package Agent;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import jade.core.AID;
import jade.core.Agent;
import Perception.PerceptsBase;
import Goal.GoalBase;

public class AgentBase extends Agent{


	protected static DocumentBuilderFactory documentbuilderfactory = DocumentBuilderFactory.newInstance();
	protected static TransformerFactory transformerfactory = TransformerFactory.newInstance();


	protected String name;
	private String host;
	private int port;
	private String username;
	private String password;
	protected String team;
	private String role;
	
	protected boolean verbose;
	protected AID aids[]; 
	
	protected PerceptsBase percepts;
	protected GoalBase goals;
	protected Connexion con;
	
	boolean actionOK;
	
	public AgentBase()
	{
		initAgent();
	}

    protected void initAgent()
    {
    	percepts = new PerceptsBase();
		goals = new GoalBase();
		actionOK=false;
    }
    
	final public void setup()
	{
		init();
		addBehaviour(new ConnexionBehaviour(percepts,con,goals,role));
		setupBehaviour();
	}

	protected void setupBehaviour()
	{
		
	}
	
	public void setAction()
	{
		actionOK = true;
	}
	
	public boolean actionToDo()
	{
		return !actionOK;
	}
	
	public String getTeamName()
	{
		return team;
	}

	public void println(String s)
	{
		System.out.println(name +" : "+s);
	}
	public void init()
	{
		System.out.println("Debut init");
		Document doc = null;
		      try
		      {
		         doc = documentbuilderfactory.newDocumentBuilder().parse(ConfigTools.conf);
		      }
		      catch(Exception e){
		    	  System.out.println(e.getMessage());
		    	  doDelete();
		    	  return;
		      }
		      System.out.println("Init nb agent");
		      NodeList list = doc.getElementsByTagName("agent");
		      aids = new AID[list.getLength()-1];
		      int index = 0;
		      System.out.println("init for");
		      for(int i = 0; i <list.getLength();i++)
		      {
		    	  Element e = (Element)(list.item(i));
		    	  System.out.println("Elem "+i+" \nname :"+e.getAttribute("name"));
		    	  if(e.getAttribute("name").equals(getLocalName()))
		    	  {
		    		  System.out.println("Start Init Attr");
		    		  name = e.getAttribute("name");
		    		  team = e.getAttribute("team");
		    		  username = e.getAttribute("username");
		    		  password = e.getAttribute("password");
		    		  host=e.getAttribute("host");
		    		  port = Integer.parseInt(e.getAttribute("port"));
		    		  verbose = Boolean.parseBoolean(e.getAttribute("verbose"));
		    		  role = e.getAttribute("role");
		    		  System.out.println("init con");
		    		  con = new Connexion(username,password,host,port);
		    		  System.out.println("init con fin");
		    		  if(verbose)
		    			  println("Name : \n" +team+ " "+username+" "+password+" "+host+" "+port+" "+verbose+" "+role);
		    	  }
		    	  else
		    	  {
		    		  aids[index] = new AID(e.getAttribute("name"),AID.ISLOCALNAME);
		    		  System.out.println("--> "+aids[index]);
		    		  index++;
		    	  }
		    		  
		      }
		      
		System.out.println("Init Fin");     
		   
	}
	
	public void initAttr()
	{
		percepts.clear();
		goals.clear();
	}
	
	public AID[] getTeamAID()
	{
		return aids;
	}
	
	public boolean isConnected()
	{
		return con.isConnected();
	}
	
	public boolean isVerbose()
	{
		return verbose;
	}
	
	public String getRole()
	{
		return role;
	}
	

}
